<template>
  <div>

    <button @click="opennew">打开新窗口</button>

    <button @click="test">插入数据</button>

    <button @click="send">发送</button>
  </div>
</template>
<script>
import { crc8, crc16, crc32 } from 'easy-crc';
export default {
  data() {


    return {
      hex: '01 03 02 03 B6',
      value: ['CRC16', 'MODBUS'],
      hexRes: null,
      type: true,

    }
  },
  mounted() {

    const net = require('net')
    console.log(net)
    let client = net.createConnection({
      host: "127.0.0.1",//本机ip
      port: 9802,//端口任意即可
    });

    client.on("connect", () => {
      client.write("node.js");
    });

    client.on("data", (buffer) => {
      console.log(buffer);
    });

    client.on("error", (err) => {
      console.log(err, "异常");
    });

    client.on("close", (err) => {
      console.log(err, "断开");

    })
    window.client = client
  },
  methods: {
    send() {
      //Buffer.from('112233', 'hex')
      window.client.write(Buffer.from('112233', 'hex'))
    },
    test() {
      const sqlite3 = require('better-sqlite3')
      // 创建数据库连接
      const db = new sqlite3('D:/mydatabase.db');

      let st = db.prepare(`INSERT INTO users (name, age) VALUES ('yux Doe', 25)`);
      st.run()
    },
    opennew() {
      this.createWindow()
    },
    createWindow() {
      console.log(process.env.NODE_ENV)
      console.log('process.env.winURL ',process.env.winURL )
      


      const electron = require('electron')
      let winUrl = electron.remote.getGlobal('sharedObject').winURL
      console.log(winUrl)


      console.log(electron)
      const mainWindow = new electron.remote.BrowserWindow({
        width: 500,
        height: 600,
        webPreferences: {
          enableRemoteModule: true,
          nodeIntegration: true,
          contextIsolation: false,
          webSecurity: false
        }
      })

      // mainWindow.loadFile('static/index.html')
      mainWindow.loadURL(winUrl + 'about')

    }
  }
}
</script>